<template>
  <el-dialog
    title="分配角色"
    :visible="showAssignRole"
    @close="btnCancel"
  >
    <!-- 多选框容器 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="end">
      <el-button @click="btnOk">确定</el-button>
      <el-button @click="btnCancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserMessage } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    showAssignRole: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 全部角色列表
      roleList: [],
      // 选中的角色列表
      checkList: [],
      userId: ''
    }
  },
  async  created() {
    const { rows } = await getRoleList({
      page: 1,
      pagesize: 999
    })
    this.roleList = rows
  },
  methods: {
    //   根据用户id 获取员工信息
    async getUserMessage(id) {
      const { roleIds } = await getUserMessage(id)
      this.checkList = roleIds
      this.userId = id
    },
    // 弹窗确认按钮
    async  btnOk() {
      // 发送请求
      await assignRoles({
        roleIds: this.checkList,
        id: this.userId
      })
      // 提示用户
      this.$message.success('修改成功')
      // 关闭弹窗
      this.$emit('update:showAssignRole', false)
    },
    // 弹窗取消按钮
    btnCancel() {
      // 防止第二次点击弹窗数据闪烁解决方二
      this.checkList = []
      // 关闭弹窗
      this.$emit('update:showAssignRole', false)
    }
  }
}
</script>

<style>

</style>
